/* eslint-disable no-else-return */
import Vue from 'vue';
import dayjs from 'dayjs';
import numeral from 'numeral';

Vue.filter('numberFormat', (value, format) => numeral(value || 0).format(format));
Vue.filter('dateFormat', (value, format) => (value ? dayjs(value).format(format) : ''));
Vue.filter('numberColor', (value) => {
  if (value > 0) {
    return 'up';
  } else if (value < 0) {
    return 'down';
  }
  return 'gray';
});
Vue.filter('publishTimeFormat', (value) => {
  let publishTime = '';
  const time = dayjs(value).format('HH:mm');
  if (dayjs().isSame(value, 'day')) {
    publishTime = `今天 ${time}`;
  } else if (dayjs().isSame(value, 'week') && dayjs().subtract(1, 'days').isSame(value, 'day')) {
    publishTime = `昨天 ${time}`;
  } else if (dayjs().isSame(value, 'week')) {
    switch (dayjs(value).day()) {
      case 0:
        publishTime = `星期日 ${time}`;
        break;
      case 1:
        publishTime = `星期一 ${time}`;
        break;
      case 2:
        publishTime = `星期二 ${time}`;
        break;
      case 3:
        publishTime = `星期三 ${time}`;
        break;
      case 4:
        publishTime = `星期四 ${time}`;
        break;
      case 5:
        publishTime = `星期五 ${time}`;
        break;
      case 6:
        publishTime = `星期六 ${time}`;
        break;
      default:
        publishTime = `星期日 ${time}`;
    }
  } else if (!dayjs().isSame(value, 'year')) {
    publishTime = dayjs(value).format('YYYY/MM/DD');
  } else {
    publishTime = `${dayjs(value).format('MM/DD HH:mm')}`;
  }
  return publishTime;
});
